﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls; 
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Fluent;
using SPCAM.Servers;
using SPCAM.Utils;

namespace SPCAM.Views
{
    /// <summary>
    /// RibbonHeader.xaml 的交互逻辑
    /// </summary>
    public partial class RibbonHeader : UserControl
    {
        public int GroupItemNum = 1;
        public RibbonHeader()
        {
            InitializeComponent();
        }
        public void Addheader(RibonHeadItem headitem)
        {
            RibbonTabItem tab = headitem.TabItem;
            SketchGroup.Header = headitem.Button.Header?.ToString()!;
            SketchGroup.Background = headitem.Color?? SketchGroup.Background;

            Fluent.Button exitbut = new Fluent.Button
            {
                Header = "退出",
                // Icon = Application.Current.TryFindResource("Close") as DrawingImage
                Icon = SpCam.Resources.ResourceUtils.PathToImg("icon/quit.png")

            };
         
            // Ribbon.ContextualGroups


            exitbut.Click += (s, e) => Server.Instance.CamHeadView = false;

            headitem.Button.Click += (s, e) => Server.Instance.CamHeadView = true;
            if (!headitem.isShow)
            {
                tab.Groups[0]?.Items.Insert(0, exitbut);
            }
           


            tab.Group = SketchGroup;
            tab.DataContext=headitem.Datacontext;
             
            Ribbonhead.Tabs.Add(tab);
            Ribbonhead.Tabs[0].Groups[^1].Items.Add(headitem.Button);
            if (headitem.isShow) Server.Instance.CamHeadView = true;


        }
        private void _ContextualGroup_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            
            if ((!(bool)e.OldValue) && (bool)e.NewValue)
            {
                var group = sender as RibbonContextualTabGroup;
                if (group?.Items.Count > GroupItemNum)
                {
                    var tabItem = group?.Items.LastOrDefault();
                    if (tabItem != null)
                    {
                        Dispatcher.BeginInvoke(() => tabItem.IsSelected = true);
                    }
                }
             
            }
        }

    }
    public class RibonHeadItem
    {
        public RibbonTabItem TabItem { get; set; }
        public Fluent.Button Button { get; set; }
        public object Datacontext { get; set; }
        public SolidColorBrush Color { get; set; }
        public bool isShow { get; set; }
    }
}
